﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;

namespace prjCompras
{
    public partial class frmConsCotacao : FormConsultaPadrao.FormConsulta
    {
        public frmConsCotacao()
        {
            InitializeComponent();
        }

        public override void tlstrpbtnPesquisar_Click(object sender, EventArgs e)
        {
            sql = @"set dateformat 'dmy';
                    select cc.Codigo, 
	                       cc.Data, 
	                       f.Nome,
	                       rc.DataEntrega, 
	                       ISNULL(rc.ValorFrete, 0) Frete,
	                       cc.CodigoRequisicao,
	                       ISNULL(SUM(rcm.QtdAtendida * rcm.ValorUnitario), 0) Total,
	                       case when rc.PedidoGerado = 0 or rc.PedidoGerado is null  then 'Não' else 'Sim' end pedido_gerado
                      from CartaCotacao cc left join
                           RespostaCotacao rc on cc.Codigo = rc.CodigoCotacao left join RespostaCotacaoMaterial rcm on rc.Codigo = rcm.CodigoRespostaCotacao,
                           Fornecedor f
                     where f.CNPJ = cc.CNPJFornecedor";

            if (chckbxData.Checked)
            {
                sql += " and cc.Data between '" + dtmpckerDataInicio.Value.ToShortDateString() + "' and '" + dtmpckerDataFim.Value.ToShortDateString() + "'";
            }

            if (txtbxFornecedor.Text != string.Empty)
            {
                sql += " and f.Nome like '" + txtbxFornecedor.Text + "%'";
            }

            if (chckbxDataEntrega.Checked)
            {
                sql += " and rc.DataEntrega between '" + dtmpckrDataEntregaInicio.Value + "' and '" + 
                    dtmpckrDataEntregaFim.Value + "'";
            }

            if (txtbxRequisicao.Text != string.Empty)
            {
                sql += " and cc.CodigoRequisicao = " + txtbxRequisicao.Text;
            }

            sql += @" group by cc.Codigo, 
	                           cc.Data, 
	                           f.Nome,
	                           rc.DataEntrega, 
	                           rc.ValorFrete,
	                           cc.CodigoRequisicao,
	                           rc.PedidoGerado";

            base.tlstrpbtnPesquisar_Click(sender, e);
            renameColumnsHeader();
        }

        private void chckbxData_Click(object sender, EventArgs e)
        {
            grpbxData.Enabled = chckbxData.Checked;
        }

        private void chckbxDataEntrega_Click(object sender, EventArgs e)
        {
            grpbxDataEntrega.Enabled = chckbxDataEntrega.Checked;
        }

        public override void tlstrpbtnLimpar_Click(object sender, EventArgs e)
        {
            chckbxData.Checked = true;
            dtmpckerDataInicio.Value = Convert.ToDateTime("01/01/3000");
            tlstrpbtnPesquisar_Click(null, null);
            dtmpckerDataInicio.Value = DateTime.Now;
            chckbxData.Checked = false;
            chckbxDataEntrega.Checked = false;
            grpbxData.Enabled = false;
            grpbxDataEntrega.Enabled = false;
            txtbxFornecedor.Text = string.Empty;
            txtbxRequisicao.Text = string.Empty;
        }

        private void renameColumnsHeader()
        {
            dtgrdvwPagina1.Columns[0].HeaderText = "Código";
            dtgrdvwPagina1.Columns[1].HeaderText = "Data Cotação";
            dtgrdvwPagina1.Columns[2].HeaderText = "Fornecedor";
            dtgrdvwPagina1.Columns[3].HeaderText = "Data Entrega";
            dtgrdvwPagina1.Columns[4].HeaderText = "Valor Frete";
            dtgrdvwPagina1.Columns[5].HeaderText = "Requisição";
            dtgrdvwPagina1.Columns[6].HeaderText = "Valor Total";
            dtgrdvwPagina1.Columns[7].HeaderText = "Pedido Gerado";
        }

        private void renameColumnsHeaderMat()
        {
            dtgrdvwMateriais.Columns[0].HeaderText = "Código";
            dtgrdvwMateriais.Columns[1].HeaderText = "Material";
            dtgrdvwMateriais.Columns[2].HeaderText = "Qtde Pedida";
            dtgrdvwMateriais.Columns[3].HeaderText = "Qtde Atendida";
            dtgrdvwMateriais.Columns[4].HeaderText = "Valor Unitário";
            dtgrdvwMateriais.Columns[5].HeaderText = "Valor Total";
        }

        private void frmConsCotacao_Load(object sender, EventArgs e)
        {
            tlstrpbtnLimpar_Click(sender, e);
            tlstrpbtnPesquisar_Click(sender, e);
        }

        public override void dtgrdvwPagina1_SelectionChanged(object sender, EventArgs e)
        {
            string cod_cot = "";
            string sql_mat = "";

            if (dtgrdvwPagina1.RowCount > 0)
            {
                cod_cot = dtgrdvwPagina1.CurrentRow.Cells[0].Value.ToString();
                sql_mat = @"select m.Codigo,
	                               m.Nome,
	                               ccm.Quantidade qtde_ped,
	                               rcm.QtdAtendida,
	                               rcm.ValorUnitario,
	                               rcm.QtdAtendida * rcm.ValorUnitario total
                              from CartaCotacaoMaterial ccm left join RespostaCotacao rc
                                     on rc.CodigoCotacao = ccm.CodigoCartaCotacao left join RespostaCotacaoMaterial rcm
                                       on rcm.CodigoRespostaCotacao = rc.Codigo,
                                   Material m       
                             where ccm.CodigoMaterial = m.Codigo
                               and (ccm.CodigoMaterial = rcm.CodigoMaterial or rcm.CodigoMaterial is null)
                               and ccm.CodigoCartaCotacao = " + cod_cot;
            }
            else
            {
                sql_mat = "Select null Codigo, null Nome, null qtde_ped, null qtde_atend, null vlunit, null vltot where 1=2";
            }
            dtgrdvwMateriais.DataSource = Conexao.getInstance.executeQuery(sql_mat);
            renameColumnsHeaderMat();
            base.dtgrdvwPagina1_SelectionChanged(sender, e);
        }

    }
}
